package ma.ensias.healthme;


import java.io.Serializable;
import java.util.Iterator;
import java.util.List;

import ma.ensias.healthme.entities.PointTrait;
import android.graphics.Color;
import android.location.Location;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.view.MenuItem;
import android.widget.Toast;

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesClient;
import com.google.android.gms.location.LocationClient;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.CameraPosition;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
import com.google.android.gms.maps.model.PolylineOptions;

public class MapActivity extends FragmentActivity implements
GooglePlayServicesClient.ConnectionCallbacks,
GooglePlayServicesClient.OnConnectionFailedListener{
	GoogleMap map;
	private final LatLng WhereIAM=new LatLng(34.0150, -6.8327);
	private LocationClient mLocationClient;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main2);
		mLocationClient = new LocationClient(this, this, this);

        map = ((SupportMapFragment) getSupportFragmentManager()
                .findFragmentById(R.id.map)).getMap();
        
        if (map == null) {
            Toast.makeText(this, "Google Maps non disponible", 
                Toast.LENGTH_LONG).show();
        }
        
        List<PointTrait> lesTraits=(List<PointTrait>)getIntent().getSerializableExtra("liste");
 
        Iterator<PointTrait> it=lesTraits.iterator();
        PointTrait p;
        int i=0;
        while(it.hasNext()){
        p=it.next();
       
        map.addMarker(new MarkerOptions()
		.position(new LatLng(p.getLatitude(), p.getLongitude()))
		.title(p.getLibelle())
		.snippet(p.getAdresse()));
        
        }
        
        CameraPosition cp=new CameraPosition.Builder()
        .target(WhereIAM)
        .zoom(7).build();
        map.animateCamera(CameraUpdateFactory.newCameraPosition(cp));
        
    }
 
   
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
 
        switch (item.getItemId()) {
 
        case R.id.menu_sethybrid:
            map.setMapType(GoogleMap.MAP_TYPE_HYBRID);
            break;
        case R.id.menu_showtraffic:
            map.setTrafficEnabled(true);
            break;
        case R.id.menu_zoomin:
            map.animateCamera(CameraUpdateFactory.zoomIn());
            break;
        case R.id.menu_zoomout:
            map.animateCamera(CameraUpdateFactory.zoomOut());
            break;
        case R.id.menu_gotolocation:
            CameraPosition cp=new CameraPosition.Builder()
            .target(WhereIAM)
            .zoom(17)
            .bearing(90).tilt(90).build();
            map.animateCamera(CameraUpdateFactory.newCameraPosition(cp));
            break;
        case R.id.menu_addmarker:
        	map.addMarker(new MarkerOptions()
        			.position(WhereIAM)
        			.title("where i am rNow!!")
        			.snippet("Rabat ...")
        			.icon(BitmapDescriptorFactory.fromResource(R.drawable.ic_launcher)));
        	break;
        case R.id.menu_getcurrentlocation:
        	map.setMyLocationEnabled(true);
        	break;
        case R.id.menu_showcurrentlocation:
        	map.setMyLocationEnabled(true);
        	Location Mine;
        	if((Mine=mLocationClient.getLastLocation())!=null){
        	LatLng myLatLng=new LatLng(Mine.getLatitude(), Mine.getLongitude());
        	CameraPosition cam=new CameraPosition.Builder()
        			.target(myLatLng).zoom(17).build();
        	
        	map.animateCamera(CameraUpdateFactory.newCameraPosition(cam));
        	map.setMyLocationEnabled(true);
        	}
        	break;
        case R.id.menu_lineconnecttwopoints:
        	
        	Location Mine1;
        	if((Mine1=mLocationClient.getLastLocation())!=null){
        	LatLng myLatLng1=new LatLng(Mine1.getLatitude(), Mine1.getLongitude());
        	
            map.addMarker(new MarkerOptions()
                    .position(myLatLng1)
                    .title("Where i'm")
                    .snippet("Room 17C")
                    .icon(BitmapDescriptorFactory.defaultMarker(
                             BitmapDescriptorFactory.HUE_AZURE))
                              );
 
            map.addPolyline(new PolylineOptions()
                .add(WhereIAM, myLatLng1).width(5).color(Color.RED));
            map.setMyLocationEnabled(true);
        	}
            break;
        }
        return true;
    }

	@Override
	protected void onStart() {
        super.onStart();
        mLocationClient.connect();
	}

	@Override
	protected void onStop() {
		mLocationClient.disconnect();
		super.onStop();
	}

	@Override
	public void onConnectionFailed(ConnectionResult arg0) {
		
	}

	@Override
	public void onConnected(Bundle arg0) {
		Log.d("", "Connected !");
	}

	@Override
	public void onDisconnected() {
		Log.d("lo", "Disconnected");
	}

}



